package org.lansir.af.database;

import java.util.Calendar;

import android.media.AudioManager;
import org.lansir.af.util.RepeatData;

/**
 * TODO 添加活动提前提醒设置
 * @author 蓝智宏
 *
 */
public class DataRowActivity extends DataRow {

	// 各个字段的索引
	public static class fid {
		public static final int ID = 0;
		public static final int Subject = 1;
		public static final int Location = 2;
		public static final int RingerMode = 3;
		public static final int StartDate = 4;
		public static final int Alarm = 5;
		public static final int RepeatType = 6;
		public static final int RepeatEvery = 7;
		public static final int RepeatEndOnDate = 8;
		public static final int Snooze = 9;
		public static final int SnoozeCount = 10;
		public static final int ActionClear = 11;
		public static final int Organizer = 12;
		public static final int OtherParticipator = 13;
	};

	// Table definition
	private final DataField[] TableDef = { new DataField(fid.ID, "_ID", DataField.Type.INT, true, true),
			new DataField(fid.Subject, "Subject", DataField.Type.TEXT, true, false), new DataField(fid.Location, "Location", DataField.Type.TEXT, true, false),
			new DataField(fid.RingerMode, "RingerMode", DataField.Type.INT, true, false),
			new DataField(fid.StartDate, "StartDate", DataField.Type.INT, true, false), new DataField(fid.Alarm, "Alarm", DataField.Type.BOOL, true, false),
			new DataField(fid.RepeatType, "RepeatType", DataField.Type.INT, true, false),
			new DataField(fid.RepeatEvery, "RepeatEvery", DataField.Type.INT, true, false),
			new DataField(fid.RepeatEndOnDate, "RepeatEndOnDate", DataField.Type.INT, true, false),
			new DataField(fid.Snooze, "Snooze", DataField.Type.INT, false, false),
			new DataField(fid.SnoozeCount, "SnoozeCount", DataField.Type.INT, false, false),
			new DataField(fid.ActionClear, "ActionClear", DataField.Type.INT, false, false),
			new DataField(fid.Organizer, "Organizer", DataField.Type.TEXT, false, false),
			new DataField(fid.OtherParticipator, "OtherParticipator", DataField.Type.TEXT, true, false) };

	// fields
	private long lActivityId;
	private String sSubject = "";
	private String sLocation = "";
	private String sOrganizer = "";
	private String sOtherParticipator = "";
	private int iRingerMode = AudioManager.RINGER_MODE_NORMAL;
	private Calendar calDateStart = Calendar.getInstance();
	private boolean bAlarm = true;
	private int iRepeatType;
	private int iRepeatEvery;
	private int iSnooze;
	private int iSnoozeCount;
	public int lActionClear = 0;
	private RepeatData rdRepeat = new RepeatData();

	public DataRowActivity(Database userdb) {
		super(userdb);
		setTableDefinition(TableDef);
	}

	@Override
	public String getTableName() {
		return Database.sTableNameActivities;
	}

	// setters
	public void setSubject(String value) {
		sSubject = new String(value.trim());
	}

	public void setOrganizer(String organizer) {
		sOrganizer = organizer;
	}
	
	public void setOtherParticipator(String otherParticipator){
		sOtherParticipator = otherParticipator;
	}

	public void setLocation(String value) {
		sLocation = new String(value.trim());
	}

	public void setRingerMode(long ringerMode) {
		this.iRingerMode = (int) ringerMode;
	}

	public void setStartDate(Calendar calDate) {
		calDateStart.setTimeInMillis(calDate.getTimeInMillis());
		calDateStart.set(Calendar.SECOND, 0);
		calDateStart.set(Calendar.MILLISECOND, 0);
	}

	public void setAlarm(boolean value) {
		bAlarm = value;
	}

	// getters
	public String getSubject() {
		return sSubject;
	}

	public String getLocation() {
		return sLocation;
	}

	public String getOrganizer() {
		return sOrganizer;
	}
	
	public String getOtherParticipator(){
		return sOtherParticipator;
	}

	public int getRingerMode() {
		return iRingerMode;
	}

	public Calendar getStartDate() {
		return calDateStart;
	}

	public boolean getAlarm() {
		return bAlarm;
	}

	public long getActivityId() {
		return lActivityId;
	}

	public void setActivityId(long activityId) {
		lActivityId = activityId;
	}

	public int getRepeatType() {
		return iRepeatType;
	}

	public void setRepeatType(int repeatType) {
		iRepeatType = repeatType;
	}

	public int getRepeatEvery() {
		return iRepeatEvery;
	}

	public int getSnooze() {
		return iSnooze;
	}

	public void setSnooze(int snooze) {
		iSnooze = snooze;
	}

	public int getSnoozeCount() {
		return iSnoozeCount;
	}

	public void setSnoozeCount(int snoozeCount) {
		iSnoozeCount = snoozeCount;
	}

	public int getActionClear() {
		return lActionClear;
	}

	public void setActionClear(int actionClear) {
		lActionClear = actionClear;
	}

	public void setRepeatEvery(int repeatEvery) {
		iRepeatEvery = repeatEvery;
	}

	public RepeatData getRepeat() {
		return rdRepeat;
	}

	public void setRepeat(RepeatData rdRepeat) {
		this.rdRepeat = rdRepeat;
	}

	@Override
	public boolean validate() {
		if (sSubject.length() > 0) {
			return true;
		}
		return false;
	}

	@Override
	public void setValuesForDataRow() {
		clearContentValues();
		value(fid.Subject).set(getSubject());
		value(fid.Location).set(getLocation());
		value(fid.Organizer).set(getOrganizer());
		value(fid.OtherParticipator).set(getOtherParticipator());
		value(fid.RingerMode).set(getRingerMode());
		value(fid.StartDate).set(getStartDate());
		value(fid.Alarm).set(getAlarm());
		value(fid.Snooze).set(getSnooze());
		value(fid.SnoozeCount).set(getSnoozeCount());
		value(fid.ActionClear).set(getActionClear());

		int iRepeatType = getRepeat().getRepeatTypeAsInt();

		value(fid.RepeatType).set(iRepeatType);
		value(fid.RepeatEvery).setNull();
		value(fid.RepeatEndOnDate).setNull();

		// if repeat type != NONE
		if (iRepeatType != 0) {
			value(fid.RepeatEvery).set(getRepeat().getEvery());
			if (getRepeat().usingEndOnDate()) {
				value(fid.RepeatEndOnDate).set(getRepeat().getEndOnDate());
			}
		}

	}

	@Override
	public void getValuesFromDataRow() {
		// subject values is not required for compare
		setActivityId(value(fid.ID).asLong());
		setSubject(value(fid.Subject).asString());
		setLocation(value(fid.Location).asString());
		setOrganizer(value(fid.Organizer).asString());
		setOtherParticipator(value(fid.OtherParticipator).asString());
		setRingerMode(value(fid.RingerMode).asLong());
		setStartDate(value(fid.StartDate).asCalendar());
		setAlarm(value(fid.Alarm).asBoolean());
		setSnooze(value(fid.Snooze).asInt());
		setSnoozeCount(value(fid.SnoozeCount).asInt());
		setActionClear(value(fid.ActionClear).asInt());

		// long lRepeatType = value(fid.RepeatType).asLong();
		this.iRepeatType = value(fid.RepeatType).asInt();

		// Log.v("ActivityFairy", "DataRowActivity repeatType:" +
		// this.iRepeatType);
		getRepeat().setRepeatTypeAsInt(this.iRepeatType);

		// if repeat type == NONE
		getRepeat().setEndOnDate(null);
		if (this.iRepeatType == 0) {
			getRepeat().setEvery(1);
		} else {
			this.iRepeatEvery = value(fid.RepeatEvery).asInt();
			getRepeat().setEvery(this.iRepeatEvery);
			if (!value(fid.RepeatEndOnDate).isNull()) {
				Calendar calDate = value(fid.RepeatEndOnDate).asCalendar();
				getRepeat().setEndOnDate(calDate);
			}
		}
	}

}
